import 'package:chathouse/constant/my_color.dart';
import 'package:chathouse/util/base_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'form_widgets.dart';

typedef MySingleSelectWidgetItemChange = Function(String? val);

//問題類型1 單選 15 單選 多列, 17单选带备注
class SingleSelectWidget1 extends StatefulWidget {
  String title;
  String? selectedValue;
  List<KeyValueBean> initValue;
  MySingleSelectWidgetItemChange onSelectedItemChange;
  int type;
  String? remark;
  bool request;

  SingleSelectWidget1(
      this.title, this.selectedValue, this.initValue, this.onSelectedItemChange,
      {this.type = 15, this.remark, this.request = false, Key? key})
      : super(key: key);

  @override
  State<SingleSelectWidget1> createState() => _SingleSelectWidget1State();
}

class _SingleSelectWidget1State extends State<SingleSelectWidget1> {
  @override
  Widget build(BuildContext context) {
    double itemW = (1.sw - 26.w * 2 - 20.w) / 2.0 - 0.1;
    return Container(
        padding: EdgeInsets.only(left: 26.w, right: 26.w),
        child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
          Row(crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              widget.request
                  ? Text(
                      "*",
                      style: getTextStyle(18.sp, color: Colors.red),
                    )
                  : Container(),
              Expanded(
                  child: Text(
                widget.title,
                style: getTextStyle(18.sp, color: Colors.black),
              ))
            ],
          ),
          SizedBox(
            height: 12.w,
          ),
          widget.type == 15
              ? Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: widget.initValue.map((e) {
                    bool isSelected = e.value == widget.selectedValue;
                    return Container(
                      padding: EdgeInsets.all(6.w),
                      constraints: BoxConstraints(
                          minWidth: itemW, maxWidth: itemW * 2 + 20.w),
                      margin: EdgeInsets.only(bottom: 20.w),
                      decoration: BoxDecoration(
                          color: isSelected
                              ? MyColors.green_204243165
                              : Colors.white,
                          border: Border.all(
                              width: 1.w, color: MyColors.gary_158157157),
                          borderRadius: BorderRadius.circular(100.w)),
                      child: Text(
                        e.name,
                        textAlign: TextAlign.center,
                        style: getTextStyle(16.sp, color: Colors.black),
                      ),
                    ).setOnClickListener(() {
                      setState(() {
                        if (widget.selectedValue == e.value) {
                          widget.selectedValue = null;
                        } else {
                          widget.selectedValue = e.value;
                        }
                      });
                      widget.onSelectedItemChange(widget.selectedValue);
                    });
                  }).toList())
              : Wrap(
                  spacing: 20.w,
                  runSpacing: 20.w,
                  children: widget.initValue.map((e) {
                    bool isSelected = e.value == widget.selectedValue;
                    return Container(
                      padding: EdgeInsets.all(6.w),
                      constraints: BoxConstraints(
                          minWidth: itemW, maxWidth: itemW * 2 + 20.w),
                      decoration: BoxDecoration(
                          color: isSelected
                              ? MyColors.green_204243165
                              : Colors.white,
                          border: Border.all(
                              width: 1.w, color: MyColors.gary_158157157),
                          borderRadius: BorderRadius.circular(100.w)),
                      child: Text(
                        e.name,
                        textAlign: TextAlign.center,
                        style: getTextStyle(16.sp, color: Colors.black),
                      ),
                    ).setOnClickListener(() {
                      setState(() {
                        if (widget.selectedValue == e.value) {
                          widget.selectedValue = null;
                        } else {
                          widget.selectedValue = e.value;
                        }
                      });
                      widget.onSelectedItemChange(widget.selectedValue);
                    });
                  }).toList(),
                ),
          widget.remark != null
              ? Row(mainAxisAlignment: MainAxisAlignment.center, children: [
                  Text(
                    widget.remark ?? "",
                    style: getTextStyle(17.sp, color: MyColors.red_2166474),
                  ),
                ]).setContainer(margin: EdgeInsets.only(top: 12.w))
              : Container()
        ]));
  }
}
